package goylc

import "sort"

// NFA 到 DFA 的转换
// 能从 NFA 的状态 s 开始只通过 ε 达到的 NFA 状态集合
func eClosureState(s GoylcState, nfa *GoylcNFA) *GoylcStateSet {
	nfa.CleanStateMark()

	set := NewGoylcStateSet()
	set_need_judge := NewGoylcStateSet()
	set_need_judge.Add(&s)

	// nexts_e := nfa.jump_table.Next(s)

	for set_need_judge.Len() > 0 {
		state_ref := set_need_judge.Pop()
		state_ref.Marked = true
		set.Add(state_ref)

		for _, pair := range nfa.jump_table.Next(state_ref.Id) {
			if pair.Alpha == GoylcAlphaEmpty {
				ns := nfa.GetStateById(pair.StateId)
				if ns == nil {
					panic("错误的语法定义，未知的状态ID " + string(pair.StateId))
				}
				if ns.Marked == false {
					set_need_judge.Add(ns)
				}
			}
		}

	} // end of set_need_judge

	return set
}

func stateSetToOrderedList(set *GoylcStateSet, comparer StateOrderComparer) []*GoylcState {
	ss := set.ToSlice()
	sort.Slice(ss, func(i, j int) bool {
		a := ss[i]
		b := ss[j]
		return comparer(a, b)
	})
	return ss
}

func convertNFAtoDFA(nfa *GoylcNFA) *GoylcDFA {

	return nil
}
